﻿Public Class Apostas
    Inherits System.Web.UI.Page

    Dim BL As New BusinessLogic.BLApostas.BLApostas
    Dim ColumnTotal As Decimal = 0
    Dim Enabled As Boolean = False

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        lblErro.Text = String.Empty

        If Not Page.IsPostBack Then

            CarregarDDL()

            rblVerApostar.Visible = False

        End If

    End Sub

    Private Sub PreecheDados()

        Dim dt As DataTable = BL.ObterJornadaDescricao(CInt(Session("IDUtilizador").ToString), ddlApostas.SelectedValue)

        grdJogos.DataSource = dt
        grdJogos.DataBind()

    End Sub

    Private Sub CarregarDDL()

        ddlApostas.DataSource = BL.ObterJornadas
        ddlApostas.DataTextField = "Descricao"
        ddlApostas.DataValueField = "IDJornada"
        ddlApostas.DataBind()

        Dim selec As New ListItem("Selecione uma Jornada.", 0)
        ddlApostas.Items.Insert(0, selec)


    End Sub

    Protected Sub rblVerApostar_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rblVerApostar.SelectedIndexChanged

        Select Case rblVerApostar.SelectedValue

            Case "VER"

                grdJogos.Columns(4).Visible = True
                grdJogos.Columns(5).Visible = True

            Case "APOSTAR"

                grdJogos.Columns(4).Visible = False
                grdJogos.Columns(5).Visible = False

        End Select

    End Sub

    Protected Sub ddlApostas_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlApostas.SelectedIndexChanged

        If ddlApostas.SelectedValue <> 0 Then

            grdJogos.Visible = True
            PreecheDados()
        Else

            grdJogos.Visible = False
        End If

    End Sub

    Protected Sub grdJogos_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdJogos.RowCommand

        Select Case e.CommandName.ToString

            Case BusinessLogic.BLAtributes.BLAtributes.OperacoesApostas.Apostar

                Dim sb As New StringBuilder

                For index = 0 To grdJogos.Rows.Count - 1

                    Dim r As GridViewRow = grdJogos.Rows(index)
                    Dim validado As String = String.Empty

                    If r.RowType = DataControlRowType.DataRow Then

                        Dim Aposta1 As String = DirectCast(r.FindControl("txtAposta1"), TextBox).Text
                        Dim Aposta2 As String = DirectCast(r.FindControl("txtAposta2"), TextBox).Text
                        Dim Realizado As String = DirectCast(r.FindControl("lblRealizado"), Label).Text
                        Dim NumJogo As String = DirectCast(r.FindControl("lblNumJogo"), Label).Text
                        Dim IDCampeonato As String = DirectCast(r.FindControl("lblIDCampeonato"), Label).Text
                        Dim IDAposta As String = DirectCast(r.FindControl("lblIDAposta"), Label).Text

                        If Realizado.Equals(BusinessLogic.BLAtributes.BLAtributes.EstadoJogo.PorRealizar) Then

                            validado = BL.ValidarApostas(Aposta1, Aposta2, NumJogo)

                            If validado.Length = 0 Then

                                validado = BL.Apostar(IDAposta, CInt(Session("IDUtilizador").ToString), IDCampeonato, ddlApostas.SelectedValue, NumJogo, Aposta1, Aposta2)

                            End If

                        End If

                    End If

                    sb.Append(validado)

                Next

                lblErro.Visible = True
                lblErro.Text = sb.ToString
                litErro.Text = sb.ToString

            Case Else

                lblErro.Visible = True
                lblErro.Text = "Aconteceu kk coisa que nao devia!"

        End Select

    End Sub

    Protected Sub grdJogos_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdJogos.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            ColumnTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Pontos"))

            If Convert.ToString(DataBinder.Eval(e.Row.DataItem, "EstadoJogo")).Equals(BusinessLogic.BLAtributes.BLAtributes.EstadoJogo.PorRealizar) Then

                enabled = True
            End If

        ElseIf e.Row.RowType = DataControlRowType.Footer Then

            Dim TotalPontos As TextBox = DirectCast(e.Row.FindControl("txtTotalPontos"), TextBox)
            TotalPontos.Text = ColumnTotal

            Dim Apostar As LinkButton = DirectCast(e.Row.FindControl("lnkBtnApostar"), LinkButton)
            Apostar.Enabled = Enabled

        End If

    End Sub

End Class